package cc.telecomdigital.tdstock.Tools;

import java.util.Calendar;
import android.os.SystemClock;

import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;

// Just a single time storage object, serve as a struct
public class CDRRecordItem
{
	private Calendar  startTime;
	private Calendar  endTime;
	private Calendar  lastAccessTime;
	private Calendar  tmpTime;
	
	private long      startRunningTick;
	private long      endRunningTick;
	private long      lastReceiveTick;
	private long      tmpTick;

	private String    lsTimeStr;

	private final String LOG_TAG = "CDRRecordItem";
	private final boolean debugMode = false;

	public CDRRecordItem(String iLsTimeStr)
	{
		ResetInfo();
		lsTimeStr = iLsTimeStr;
	}

	/** Reset all param, need to set the startingTime and startRunningTime **/
	public void ResetInfo()
	{
		startTime        = null;
		endTime          = null;
		lastAccessTime   = null;
		tmpTime          = null;
		startRunningTick = 0;
		endRunningTick   = 0;
		lastReceiveTick  = 0;
		tmpTick          = 0;

		if (debugMode)
			TDStockLog.i(LOG_TAG, "ResetInfo");
	}

	/** Start Running Time could only be set once **/	
	public void SetStartRunningTime()
	{
		if (startTime != null)
			return;

		startTime 	  = GetCurTime();
		startRunningTick = GetCurTick();
		if (debugMode)
			TDStockLog.i(LOG_TAG, "SetStartRunningTime");				
	}

	/** If no start Running Time, we can not set the endRunning Time **/
	public void SetEndRunningTime()
	{
		if (startTime == null)
			return;

		endTime        = GetCurTime();
		endRunningTick = GetCurTick();
		if (debugMode)
			TDStockLog.i(LOG_TAG, "SetEndRunningTime");		
		
	}

	/** Use the current time to set as the last data received time **/
	public void SetLastAccessTime()
	{
		if (startTime == null)
			return;
		
		lastAccessTime  = GetCurTime();
		lastReceiveTick = GetCurTick();
		if (debugMode)
			TDStockLog.i(LOG_TAG, "SetLastAccessTime");	
	}

	/** System Clock that could be modified by outsider **/
	private Calendar GetCurTime()
	{
		return Calendar.getInstance();
	}

	/** More like a Crystal in the system, in millisecond **/
	private long GetCurTick()
	{
		return SystemClock.elapsedRealtime();
	}

	public Calendar GetStartRunningTime()
	{
		return startTime;
	}
	
	/** Tick Record when StartRunning is set, in millisecond **/	
	public long GetStartRunningTick()
	{
		return startRunningTick;
	}

	public Calendar GetEndRunningTime()
	{
		return endTime;
	}	
	
	/** Tick Record when EndRunningTime is set, in millisecond **/	
	public long GetEndRunningTick()
	{
		return endRunningTick;	
	}

	public Calendar GetLastAccessTime()
	{
		return lastAccessTime;
	}	
	
	/** Tick Record base on system on last access of the data, in millisecond **/
	public long GetLastAccessTick()
	{
		return lastReceiveTick;
	}

	/** Light Streamer Time that set previously **/
	public String GetLightStreamerTimeString()
	{
		return lsTimeStr;
	}	

	/** Will try to update if the previous one is not a correct one **/
	public void SetLightStreamerTimeString(String iNewTimeString)
	{
		if ((lsTimeStr == null) || lsTimeStr.equals(""))	
			lsTimeStr = iNewTimeString;
	}
	
	/** Save a test record into memory, use it by calling RetreiveLastCheck Value **/
	public long CheckUpdateTick()
	{		
		if (startRunningTick == 0)
			return 0;
		
		long diffTick;
		
		tmpTime = GetCurTime();
		tmpTick = GetCurTick();
		
		diffTick = tmpTick - startRunningTick;

		if (debugMode)
			TDStockLog.i(LOG_TAG, "CheckUpdateTick=>Result: " + diffTick);
		
		return diffTick;		
	}
	
	/** Overrid the pre-save value **/
	public boolean RetrieveLastCheckValue()
	{
		if (tmpTick == 0)
			return false;
		
		//Adjust the Tick
		endRunningTick  = tmpTick;
		lastReceiveTick = tmpTick;

		//Adjust the Time		
		endTime         = tmpTime;		
		lastAccessTime  = tmpTime;
		
		//Reset the last Info
		tmpTime = null;
		tmpTick = 0;
		
		return true;
	}
	
	public boolean AdjustToLastCheckValue(long iDiffValue)
	{
		if (tmpTick == 0)
			return false;
		
		//Adjust the Tick		
		endRunningTick  = startRunningTick + iDiffValue;
		lastReceiveTick = endRunningTick;
		
		//Adjust the Time
		long newTick;
		
		newTick = startTime.getTimeInMillis() + iDiffValue;
		endTime = GetCurTime();
		endTime.setTimeInMillis(newTick);
		lastAccessTime = endTime;

		//Reset the last Info
		tmpTime = null;
		tmpTick = 0;

		return true;
	}
		
	//This is to create a cross over ticker;
	public CDRRecordItem(CDRRecordItem iNewItem)
	{		
		lsTimeStr        = iNewItem.lsTimeStr;
		
		//Copy the Tick
		startRunningTick = iNewItem.endRunningTick;
		lastReceiveTick  = startRunningTick;		
		endRunningTick   = 0;
		
		//copy the Time
		startTime      = iNewItem.endTime;
		lastAccessTime = startTime;
		endTime        = null;
		
		//Flush the previous
		tmpTime = null;
		tmpTick = 0;
		
		if (debugMode)
			TDStockLog.i(LOG_TAG, "**Date Recreated with previous");
	}
	
}
